import Player from "./player";
import UndirectedGraph from "./UndirectedGraph"

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    mapGraph: UndirectedGraph

    onLoad() {
        this.initMap()
        cc.find('Canvas/bg').on(cc.Node.EventType.TOUCH_START, this.handleTouchMap, this)
    }

    start() {
        // this.findPath()
    }

    initMap() { // 初始化无向图，添加顶点和边
        const mapLayer = cc.find('Canvas/map')

        this.mapGraph = new UndirectedGraph()

        for (let i = 0; i < mapLayer.children.length; i++) {
            const name = i + 1 + ''
            const node = cc.find(name, mapLayer)

            this.mapGraph.addVertex(name, node)
        }

        this.mapGraph.addEdge('1', '2')
        this.mapGraph.addEdge('1', '18')
        this.mapGraph.addEdge('2', '3')
        this.mapGraph.addEdge('2', '5')
        this.mapGraph.addEdge('3', '4')
        this.mapGraph.addEdge('5', '6')
        this.mapGraph.addEdge('5', '15')
        this.mapGraph.addEdge('6', '7')
        this.mapGraph.addEdge('6', '9')
        this.mapGraph.addEdge('7', '8')
        this.mapGraph.addEdge('9', '10')
        this.mapGraph.addEdge('10', '11')
        this.mapGraph.addEdge('10', '12')
        this.mapGraph.addEdge('12', '13')
        this.mapGraph.addEdge('13', '14')
        this.mapGraph.addEdge('13', '15')
        this.mapGraph.addEdge('15', '16')
        this.mapGraph.addEdge('16', '17')
        this.mapGraph.addEdge('16', '18')
    }

    getPath(startName: string, endName: string) {
        const path = this.mapGraph.findPath(startName, endName)
        const formatStr = this.formatPath(path)

        console.log(`起点：${startName}，终点：${endName}`)
        console.log(`路径：${formatStr}`)

        return path.map(i => i.node)
    }

    formatPath(pathArr) {
        let res = ''
        pathArr.forEach(n => {
            res += '->'
            res += n.name
        })
        return res
    }

    getNearestMapNodeName(position: cc.Vec2) {
        const mapLayer = cc.find('Canvas/map')
        let nearestNode = mapLayer.children[0]

        mapLayer.children.forEach(node => {
            const dis1 = node.getPosition().sub(position).mag()
            const dis2 = nearestNode.getPosition().sub(position).mag()

            if (dis1 < dis2) {
                nearestNode = node
            }
        })

        return nearestNode.name
    }

    handleTouchMap(e) {
        const mapLayer = cc.find('Canvas/map')
        const player = cc.find('Canvas/player')
        const touchPoint = mapLayer.convertToNodeSpaceAR(e.touch.getLocation())
        const playerPoint = mapLayer.convertToNodeSpaceAR(player.parent.convertToWorldSpaceAR(player.getPosition()))

        const startName = this.getNearestMapNodeName(playerPoint)
        const endName = this.getNearestMapNodeName(touchPoint)

        const path = this.getPath(startName, endName)

        player.getComponent(Player).walkAlongThePath(path)

        // console.log(startName, endName)
        // console.log('离触摸点最近的地图节点名：', endName)
    }

    // update (dt) {}
}
